package com.ys.controller.api.purchase;


import com.ys.config.MenuId;
import com.ys.entity.purchase.TcgPurchaseAddress;
import com.ys.entity.purchase.TcgPurchaseAddress;
import com.ys.entity.purchase.TcgPurchaseAddress;
import com.ys.entity.sys.YsCol;
import com.ys.service.purchase.TcgPurchaseAddressService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 采购地址维护
 */
@Controller
@RequestMapping("/api/inter/tcgPurchaseAddress")
public class ApiInterTcgPurchaseAddressController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcgPurchaseAddressController.class);

    @Autowired
    private CommonApiService<TcgPurchaseAddress> commonApiService;
    //菜单id
    private Long menuId = MenuId.TCG_PurchaseAddress;
    @Autowired
    private TcgPurchaseAddressService tcgPurchaseAddressService;
    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcgPurchaseAddress setSeachSql(TcgPurchaseAddress cond){

        cond.addLeftJoin("TS_UserChange U ON U.Id = A.CreateUser ")
                .addField("U.UserId + '/' + U.UserName AS createUser");//创建人

        cond.addField("A.VGjH + ',' + A.VSfH + ',' + A.VDsH + ',' + A.VCqH + ',' + A.VDzH  AS addressDetail");//地址详情
        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }
    /**
     * 查询
     */
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcgPurchaseAddress cond, String conditions
    ){
        try {
            cond = setSeachSql(cond);

            LayuiTableVoJA setList = commonApiService.searchLay(cond);

            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 采购地址维护弹出框
     */
    @RequestMapping("/modalBySearch")
    @ResponseBody
    public LayuiTableVoJA modalBySearch(com.bo.annotation.Model model, HttpServletRequest request, HttpServletResponse response,
                                           @ModelAttribute TcgPurchaseAddress cond
    ) {
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
            return tableVoJA;
        } catch (Exception e) {
            logger.error("查询出错：", e.getMessage(), e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }

    }
    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcgPurchaseAddress cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
//            //查询条件
//            String nameLinked = ConditionUtil.getConditionValue(conditions, "createUser");//制单人
//            if (nameLinked != null){
//                cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
//            }
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcgPurchaseAddress> util = new ExportExcelUtils<>(new TcgPurchaseAddress());
            String toFilePath = util.exportExcel(listData, listField, "采购地址维护");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }
    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增
            return tcgPurchaseAddressService.add(params,menuId);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }

    /**
     * 编辑
     */
    @PostMapping("/toEdit")
    @ResponseBody
    public R edit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData){
        try {
            return tcgPurchaseAddressService.edit(params, MenuId.TCG_PurchaseAddress,subData);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

}